import {
  addressSplit,
  // addressCut,
  addressCount,
  addressComplement,
  stringFileName,
  arrayFromString,
} from '@vimi/utils-tool'

export const enumGuaType = [
  { label: '联担业务', value: '4' },
  { label: '直担业务', value: '2' },
  { label: '限率业务', value: '3' },
]

export const enumProductType = {
  1: '金融',
  2: '非金融',
}

export const provinceGoodsServiceId = 'COMMON_PRODUCT'

// 产品的区域、所属关系的查询参数
export const getParams = (user, org) => {
  // const isProvince = user.orgRegionLever === 1
  const params = {}
  params.supply_agency_id = org.clientId
  // if (isProvince) {
  //   // 省不查看市添加的产品，所有只查默认的
  //   params['service_id.eq'] = provinceGoodsServiceId
  // } else {
  //   // 市担保可以查看省担保的产品（适用区域包括市，审批通过，只能查看不能修改）
  //   const queryAddress = addressSplit(user.queryAddressCode)
  //   const { length } = queryAddress
  //   if (length) {
  //     queryAddress[length - 1] = addressCut(queryAddress[length - 1])
  //   }
  //   params['orWhere.supply_address_code.like'] = queryAddress

  //   params.orWhereAndTemplate = 1
  //   params['orWhereAnd.service_id.eq'] = org.clientId
  //   params['orWhereAnd.service_id.neq'] = provinceGoodsServiceId

  //   params['orWhereAnd.service_id.in'] = provinceGoodsServiceId
  //   params['orWhereAnd.is_approval.eq'] = 1
  // }
  return params
}

export const getPolicyFilterAddress = queryAddressCode => {
  const addressArr = addressSplit(queryAddressCode)
  const cutAddress = queryAddressCode.replace(/0+$/, (w, i) =>
    Array(addressComplement[i] + 1).join('0')
  )
  const nextAddress = `${Number.parseInt(cutAddress) + 1}`.padEnd(addressCount, '0')
  return {
    curr: queryAddressCode,
    next: nextAddress,
    sups: addressArr,
  }
}

export const dataAdapter = item => {
  item.shelves_status = +item.shelves_status
  item.is_approval = +item.is_approval
  item.supply_product = (item.supply_product || '').split(',')
  item.scaleLabel = `${item.scale || ''}(${item.uscale || ''})`
  const type = enumGuaType.find(t => +t.value === +item.gua_type) || {}
  item.guaType = type.label || ''
  item.reportUrls = arrayFromString(item.ind_repor_url).map(url => ({
    url,
    name: stringFileName(url),
  }))

  return item
}
